package com.ailk.kbi.kbirazor.component
{
	import flash.events.Event;
	
	import mx.collections.ArrayCollection;
	import mx.core.IVisualElement;
	
	import spark.components.VGroup;
	
	public class ChildrenNodeGroup extends VGroup
	{
		private var _contentXML:XML;
		
		public function ChildrenNodeGroup(contentXML:XML)
		{
			super();
			_contentXML = contentXML;
			updateChildren();
		}
		
		public function get ancherY():ArrayCollection {
			var ancherYArray:ArrayCollection = new ArrayCollection();
			for(var index:int = 0; index < numElements; index++){
				var element:NodeGroup = getElementAt(index) as NodeGroup;
				ancherYArray.addItem(element.y + element.height/2);
			}
			return ancherYArray;
		}
		
		public function get nodeGroupList():ArrayCollection {
			var _nodeGroupList:ArrayCollection = new ArrayCollection();
			for(var index:int = 0; index < numElements; index++){
				var element:NodeGroup = getElementAt(index) as NodeGroup;
				_nodeGroupList.addItem(element);
			}
			return _nodeGroupList;
		}
		
		private function updateChildren():void{
			gap = 10;
			paddingTop = 1;
			paddingBottom = 0;
			
			for each(var childrenXML:XML in _contentXML.node){
				/*添加子节点*/
				var newNodeGroup:NodeGroup = new NodeGroup(childrenXML);
				newNodeGroup.addEventListener("widthChanged", widthChangedHandler);
				newNodeGroup.addEventListener("heightChanged", heightChangedHandler);
				addElement(newNodeGroup);	
			}
		}
		
		private function widthChangedHandler(event:Event):void {
			var newWidth:Number = 0;
			for(var index:int = 0; index < numElements; index++){
				var element:NodeGroup = getElementAt(index) as NodeGroup;
				newWidth = Math.max(newWidth, element.width);
			}
			width = newWidth;
		}
		
		private function heightChangedHandler(event:Event):void {
			var newHeigth:Number = paddingTop + paddingBottom + (numElements - 1) * gap;
			for(var index:int = 0; index < numElements; index++){
				var element:NodeGroup = getElementAt(index) as NodeGroup;
				newHeigth = newHeigth + element.height;
			}
			height = newHeigth;
			
		}
	}
}